/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package racer.client.data;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;


/**
 *
 * @author Lieven
 */
public class FileHandlers {

    
public static String openFileHandler(File fileToOpen){
            return getMapAsString(fileToOpen);
            /*{
                 //label.setText("<html><font color='green'>CONFIRMATION: The Project has been successfully opened! </font></html>");                      
                //canvas.repaint();
                System.out.println("FILE SUCCESFULLY LOADED");
            }else{
                 System.out.println("LOADING FAILED");
                //label.setText("<html><font color='red'>ERROR: loading failed (perhaps empty or 'map.txt' file)!</font></html>");             
            } */               
    
}    
public static boolean saveFileHandler(String mapFileName,String mapstring){
         System.out.println("SAVE");
         return saveMapStringAsFile(mapFileName,mapstring);
    
}


  private static boolean saveMapStringAsFile(String mapFileName,String mapstring){
     try{
   
         
        FileWriter fstream = new FileWriter(new File(mapFileName));
        BufferedWriter out = new BufferedWriter(fstream);
    
        Matcher vergelijk = RegEx.getMatcher("n",mapstring);
        mapstring = vergelijk.replaceAll("\n");                                        
        out.write(mapstring);
        System.out.println(mapFileName);
        System.out.println(mapstring);
  
        out.close();
        return true;
    }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
      return false;
    }
     
      
  }  
  
   private static String getMapAsString(File mapInProgressFile){
       String file2String="";
       
       //File mapInProgressFile = new File(filename);
    try {
      //use buffering, reading one line at a time
      //FileReader always assumes default encoding is OK!
      BufferedReader input =  new BufferedReader(new FileReader(mapInProgressFile));
      try {
        String line = null; //not declared within while loop
        /*
        * readLine is a bit quirky :
        * it returns the content of a line MINUS the newline.
        * it returns null only for the END of the stream.
        * it returns an empty String if two newlines appear in a row.
        */
        while (( line = input.readLine()) != null){
                 
                  
      //RE magic = new RE("\\s");
      
      //String result = magic.subst( line, "s" );
      //System.out.println( result );
 
 //     String all = magic.substituteAll( line, replacement );
            file2String+=line+"n";
   
        } 
        
      }
      finally {
        input.close();
        System.out.println("done");
        return file2String;
        //System.exit(0);
            
      }
    }
    catch (IOException ex){
      ex.printStackTrace();
      return "";
    }
   }


}//end class